package com.cn.lotary.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Jason Chen
 */
public class DaoControllerProvider {

    private static final String HELP = "USAGE\n"
            + "\t auto create dao or controller class \n"
            + "\t must input 3 param \n"
            + "\t OPTIONS \n"
            + "\t\t  -dao entitypath daopath \n"
            + "\t\t  OR \n"
            + "\t\t  -controller entitypath controllerpath \n"
            + "\t auto create dao calss example: \n"
            + "\t  -dao /home/entity /home/dao \n";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        int len = args.length;

        if (len == 3) {
            String first = args[0];
            String second = args[1];
            String thrid = args[2];

            if (first.equals("help")) {
                System.out.println(HELP);
            } else if (first.equals("-dao")) {
                providerClass(second, thrid, ClasType.Dao);
            } else if (first.equals("-controller")) {
                providerClass(second, thrid, ClasType.Controller);
            } else {
                System.out.println(HELP);
            }

        } else {
            System.out.println(HELP);
        }
    }

    /**
     *
     * @param entityPath 实体java路径
     * @param daoControllerPath 生成
     */
    public static void createDaoAndController(String entityPath, String daoControllerPath) {
        try {
            providerClass(entityPath, entityPath, ClasType.Dao);
            providerClass(entityPath, entityPath, ClasType.Controller);
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }

    private static void providerClass(String entityPath, String newPath, ClasType extension) throws IOException {
        File file = new File(entityPath);
        if (!file.exists()) {
            System.out.println("the path [" + entityPath + "] deso not exists !");
            return;
        }
        for (File f : file.listFiles()) {
            String entityName = f.getName();
            newPath = newPath + "/" + extension.name();
            File l = new File(newPath);
            if (!l.exists()) {
                l.mkdirs();
            }
            if (entityName.contains(".java")) {
                String fileanme = entityName.substring(0, entityName.indexOf("."));

                String newfullpath = newPath + "/" + fileanme + extension.name() + ".java";

                File newfile = new File(newfullpath);
                if (!newfile.exists()) {
                    newfile.createNewFile();
                }

                FileWriter fileWriter = new FileWriter(newfile, true);

                fileWriter.write(extension.equals(ClasType.Dao) ? daoClassModel(fileanme) : controllerClassModel(fileanme));
                fileWriter.close();
            }
        }
    }

    private static String daoClassModel(String entityname) {
        String outcome =
                "package com.cn.lotary.dao;\r\n"
                + "\r\n"
                + "import com.cn.lotary.base.dao.BaseDao;\r\n"
                + "import com.cn.lotary.base.dao.BaseDaoParam;\r\n"
                + "import com.cn.lotary.entity." + entityname + ";\r\n"
                + "import javax.ejb.Stateless;\r\n"
                + "import javax.persistence.EntityManager;\r\n"
                + "import javax.persistence.PersistenceContext;\r\n"
                + "\r\n"
                + "/**\r\n"
                + " *\r\n"
                + " * @author By ClassProvider\r\n"
                + " */\r\n"
                + "@Stateless\r\n"
                + "public class " + entityname + "Dao extends BaseDao<" + entityname + "> {\r\n"
                + "\r\n"
                + "    @PersistenceContext\r\n"
                + "    private EntityManager em;\r\n"
                + "\r\n"
                + "    @Override\r\n"
                + "    protected BaseDaoParam getBaseDaoParam() {\r\n"
                + "        return new BaseDaoParam<" + entityname + ">(em, " + entityname + ".class, \"getId\");\r\n"
                + "    }\r\n"
                + "}\r\n";

        return outcome;
    }

    private static String controllerClassModel(String entityname) {
        String outcome =
                "package com.cn.lotary.web.controller;\r\n"
                + "\r\n"
                + "import com.cn.lotary.base.dao.BaseDao;\r\n"
                + "import com.cn.lotary.base.web.BaseController;\r\n"
                + "import com.cn.lotary.dao." + entityname + "Dao;\r\n"
                + "import com.cn.lotary.entity." + entityname + ";\r\n"
                + "import javax.ejb.EJB;\r\n"
                + "import javax.faces.bean.ManagedBean;\r\n"
                + "import javax.faces.bean.ViewScoped;\r\n"
                + "\r\n"
                + "/**\r\n"
                + " *\r\n"
                + " * @author Auto Provider \r\n"
                + " */\r\n"
                + "@ManagedBean\r\n"
                + "@ViewScoped\r\n"
                + "public class " + entityname + "Controller extends BaseController<" + entityname + "> {\r\n"
                + "\r\n"
                + "    @EJB\r\n"
                + "    private " + entityname + "Dao dao;\r\n"
                + "\r\n"
                + "    @Override\r\n"
                + "    protected BaseDao<" + entityname + "> dao() {\r\n"
                + "        return dao;\r\n"
                + "    }\r\n"
                + "\r\n"
                + "    @Override\r\n"
                + "    protected String selectItemLabel() {\r\n"
                + "          //TODO:check return value\r\n"
                + "          return \"getName\";\r\n"
                + "    }\r\n"
                + "}\r\n";
        return outcome;
    }

    /**
     * remove id column @NotNull
     *
     * @param entityPath
     */
    private static void remvoeNotNull(String entityPath) {
        File files = new File(entityPath);
        if (files.isDirectory()) {
            for (File entity : files.listFiles()) {
                String name = entity.getName();
                if (name.endsWith(".java")) {
                    try {
                        //TODO: remove id @notNull
                        Scanner scanner = new Scanner(entity);
                        String newStr = "";
                        while (scanner.hasNext()) {
                            newStr = newStr + scanner.nextLine() + "\r\n";
                        }

                        newStr = newStr.replace("@NotNull\r\n    @Column(name = \"id\"", "@Column(name = \"id\"");
                        entity.delete();
                        FileWriter writer = new FileWriter(entity);
                        writer.write(newStr);
                        writer.close();
                        scanner.close();
                    } catch (Exception ex) {
                        System.err.println("remvoeNotNull:  " + ex.getLocalizedMessage());
                    }
                }
            }
        }

    }
}

